package 哈希表_有序表_比较器;

import java.util.PriorityQueue;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * @author ljb
 * @version 1.0
 * @Date 2024/11/24
 */
public class Code02_TreeSetAndTreeMap {
    public static void main(String[] args) {
    //    底层红黑树
        TreeMap<Integer, String> treeMap = new TreeMap<>();
        treeMap.put(5, "这是5");
        treeMap.put(7, "这是7");
        treeMap.put(1, "这是1");
        treeMap.put(2, "这是2");
        treeMap.put(3, "这是3");
        treeMap.put(4, "这是4");
        treeMap.put(8, "这是8");

        System.out.println(treeMap.containsKey(1));
        System.out.println(treeMap.containsKey(10));
        System.out.println(treeMap.get(4));
        treeMap.put(4, "张三是4");//k值相同，v值被替代
        System.out.println(treeMap.get(4));

        treeMap.remove(4);
        System.out.println(treeMap.get(4) == null);

        //TreeMap，根据k值进行排序

        // TreeMap中，所有的key，<= 4且最近的key是什么
        System.out.println(treeMap.firstKey());
        // TreeMap中，所有的key，>= 4且最近的key是什么
        System.out.println(treeMap.lastKey());

        System.out.println(treeMap.floorKey(4));
        System.out.println(treeMap.ceilingKey(4));

        System.out.println("==========================");

        //自动去重
        TreeSet<Integer> set = new TreeSet<>();
        set.add(3);
        set.add(3);
        set.add(4);
        set.add(4);
        System.out.println("有序表大小：" + set.size());
        while(!set.isEmpty()){
            System.out.println(set.pollFirst());
            // System.out.println(set.pollLast());
        }

        System.out.println("==========================");

    //    堆，默认是小根堆，如果要大根堆，定制比较器
        PriorityQueue<Integer> heap1 = new PriorityQueue<>();
        heap1.add(3);
        heap1.add(3);
        heap1.add(4);
        heap1.add(4);
        System.out.println("堆大小：" + heap1.size());
        while(!heap1.isEmpty()){
            System.out.println(heap1.poll());
        }

    //    定制大根堆，用比较器
        PriorityQueue<Integer> heap2
                = new PriorityQueue<>((a, b) -> b-a);
        heap2.add(3);
        heap2.add(3);
        heap2.add(4);
        heap2.add(4);
        System.out.println("堆大小：" + heap2.size());
        while(!heap2.isEmpty()){
            System.out.println(heap2.poll());
        }
    }
}
